using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class edit_category_to_category : System.Web.UI.Page
{
    //string select = "";
    //DataTable table = new DataTable();
    //DataTable privilege_table = new DataTable();

    string user_present = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        // test user login yet
        user_present = (string)Session["user"];
        if (user_present == null)
        {
            Response.Redirect("../main/login.aspx");
        }

        if (Page.IsPostBack == false)
        {
            load_dropdownlist(ddlCategoryName);
            load_data();
        }
    }

    protected void load_dropdownlist(DropDownList ddl)
    {
        ddl.DataSource = Category.get_all_category();
        ddl.DataTextField = "CategoryName";
        ddl.DataValueField = "CategoryID";
        ddl.DataBind();
    }

    protected void load_listbox(ListBox list,int parent_id)
    {
        list.DataSource = Category.get_subcategory(parent_id);
        list.DataTextField = "CategoryName";
        list.DataValueField = "CategoryID";
        list.DataBind();
    }

    protected void dropdownlist_change(object sender, EventArgs e)
    {
        load_listbox(listbox_child,Convert.ToInt32(ddlCategoryName.SelectedValue.ToString()));
    }

    void load_data()
    {
        load_listbox(listbox_parent, 0);
        load_listbox(listbox_child, Convert.ToInt32(ddlCategoryName.SelectedValue.ToString()));
        
    }

    protected void btnAdd_Click(object sender, EventArgs e) 
    { 
        int _count = listbox_parent.Items.Count;
        if (_count != 0)
        {
            for (int i = 0; i < _count; i++)
            {
                if (listbox_parent.SelectedIndex > -1)
                {

                    string _value = listbox_parent.SelectedItem.Value;
                    //Gets the value of items in list.  
                    string _text = listbox_parent.SelectedItem.Text;
                    // Gets the Text of items in the list.  
                    ListItem item = new ListItem();
                    //create a list item  
                    item.Text = _text;
                    //Assign the values to list item  
                    item.Value = _value;
                    if (ddlCategoryName.SelectedValue == item.Value) return;

                    listbox_child.Items.Add(item);
                    //Add the list item to the selected list of employees  
                    listbox_parent.Items.Remove(item);
                    //Remove the details from employee list  
                }
            }
        }
    }

    protected void btnRemove_Click(object sender, EventArgs e)
    {
        int _count = listbox_child.Items.Count;
        if (_count != 0)
        {
            for (int i = 0; i < _count; i++)
            {
                if (listbox_child.SelectedIndex > -1)
                {
                    string _value = listbox_child.SelectedItem.Value; //Gets the value of items in list.
                    string _text = listbox_child.SelectedItem.Text;  // Gets the Text of items in the list.  
                    ListItem item = new ListItem(); //create a list item
                    item.Text = _text;               //Assign the values to list item   
                    item.Value = _value;
                    listbox_child.Items.Remove(item); //Remove from the selected list
                    listbox_parent.Items.Add(item); //Add in the Employee list 
                }
            }
        }

    }

    protected void btnRemoveAll_Click(object sender, EventArgs e)
    {
        int _count = listbox_child.Items.Count;
        if (_count != 0)
        {
            for (int i = 0; i < _count; i++)
            {
                ListItem item = new ListItem();
                item.Text = listbox_child.Items[i].Text;
                item.Value = listbox_child.Items[i].Value;
                listbox_parent.Items.Add(item);
            }
        }
        //clear the items  
        listbox_child.Items.Clear();
    }

    protected void btnAddAll_Click(object sender, EventArgs e)
    {
        int _count = listbox_parent.Items.Count;
        if (_count != 0)
        {
            for (int i = 0; i < _count; i++)
            {
                ListItem item = new ListItem();
                item.Text = listbox_parent.Items[i].Text;
                item.Value = listbox_parent.Items[i].Value;
                //Add the item to selected employee list
                listbox_child.Items.Add(item);
            }

        }

        //clear employee list
        listbox_parent.Items.Clear();

    }

    protected void save_click(object sender, EventArgs e)
    {
        string insert = "";
        int _count = listbox_child.Items.Count;
        try
        {
            for (int i = 0; i < _count; i++)
            {
                ListItem item = new ListItem();
                item.Value = listbox_child.Items[i].Value;
                Category.update_category(Convert.ToInt32(item.Value.ToString()), Convert.ToInt32(ddlCategoryName.SelectedValue.ToString()), user_present);
            }

            _count = listbox_parent.Items.Count;
            // save list_parent
            for (int i = 0; i < _count; i++)
            {
                ListItem item2 = new ListItem();
                item2.Value = listbox_parent.Items[i].Value;
                // update CATEGORY
                Category.update_category(Convert.ToInt32(item2.Value.ToString()), 0, user_present);
            }

            message_box("Save subcategory successfully !!!");

        }
        catch (Exception ex)
        {
            message_box("Save subcategory fail !!!");
        }
    }

    public void message_box(string messgage)
    {
        ClientScript.RegisterStartupScript(GetType(), "Message", "<script language='javascript'>alert('" + messgage + "');</script>");
    }
}
